package com.lzh.persevere.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.persevere.mapper.SysOperationLogMapper;
import com.lzh.persevere.model.entity.SysOperationLog;
import com.lzh.persevere.model.qo.PageModelQO;
import com.lzh.persevere.service.SysOperationLogService;
import com.lzh.persevere.template.PageAbstractTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luzhiheng
 * @since 2025-01-23
 */
@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogMapper, SysOperationLog> implements SysOperationLogService {

    @Resource
    private RedisCacheService redisCacheService;

    @Override
    public Map<String, Object> pageList(PageModelQO qo) {
        Map<String, Object> otherQueryMap = new HashMap<>();
        PageAbstractTemplate template = new PageAbstractTemplate() {
            @Override
            public Integer countTotal() {
                return baseMapper.countList(getQueryMap(qo.getPageModel(), otherQueryMap));
            }

            @Override
            public List<Map<String, Object>> queryPageList() {
                List<Map<String, Object>> pageList = baseMapper.getPageList(queryMap);
                Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();
                if (CollectionUtil.isNotEmpty(pageList)) {
                    pageList.forEach(map -> {
                        map.put("createNickName", allUserIdNickNameMap.get(String.valueOf(map.get("createUserId"))));
                        map.put("updateNickName", allUserIdNickNameMap.get(String.valueOf(map.get("updateUserId"))));
                    });
                }
                return pageList;
            }
        };
        return template.queryPage();
    }
}
